package org.fujene.memcon;

public class ClusterTuple implements Comparable<ClusterTuple>{
	private int cluster;
	private int block;
	
	public ClusterTuple(int cluster, int block){
		this.cluster = cluster;
		this.block = block;
	}
	
	public int cluster(){
		return cluster;
	}
	
	public int block(){
		return block;
	}
	
	public String fileName(){
		return String.format("%d_%d.blk", cluster, block);
	}
	
	public static ClusterTuple fromFile(String fileName){
		String[] files = fileName.split("[_\\.]");
		if(files.length != 3){System.out.println(files); return null;}
		ClusterTuple newTuple = new ClusterTuple(Integer.parseInt(files[0]), Integer.parseInt(files[1]));
		
		return newTuple;
	}
	
	@Override
	public int hashCode(){
		return (cluster << 16) + block;
	}

	@Override
	public int compareTo(ClusterTuple o){
		if(this.cluster() > o.cluster()) return 1;
		else if(this.cluster() < o.cluster()) return -1;
		else{
			if(this.block() > o.block()) return 1;
			else if(this.block() < o.block()) return -1;
		}
		return 0;
	}
	
	@Override
	public boolean equals(Object obj){
		if(!(obj instanceof ClusterTuple)) return false;
		return (this.cluster == ((ClusterTuple)obj).cluster && this.block == ((ClusterTuple)obj).block);
	}
}
